package com.geek.thread.locks;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * 死锁测试
 */
@Slf4j
public class DeadLock {

	/**
	 * 使用jstack pid查看jvm程序
	 */
	public static void main(String[] args) {
		Object obj1 = new Object();
		Object obj2 = new Object();
		new Thread("thread-1"){
			@SneakyThrows
			@Override
			public void run() {
				synchronized (obj1){
					log.info(getName()+"获取到obj1的锁，休眠");
					Thread.sleep(1000);
					synchronized (obj2){
						log.info(getName()+"获取obj2的锁");
					}
				}
			}
		}.start();
		new Thread("thread-2"){
			@SneakyThrows
			@Override
			public void run() {
				synchronized (obj2){
					log.info(getName()+"获取到obj2的锁，休眠");
					Thread.sleep(1000);
					synchronized (obj1){
						log.info(getName()+"获取obj1的锁");
					}
				}
			}
		}.start();

		log.info("主线程执行完毕");
	}

}
